<?	
	define('IN_SITE', true);
	$ids = isset($_REQUEST['ids'])? $_REQUEST['ids'] : session_id() ;
	$lng = isset($_REQUEST['lang'])? $_REQUEST['lang'] : 'us' ;
	
	$url=$_SERVER['HTTP_HOST'].$_SERVER['QUERY_STRING']; 
	if (eregi("contacts.php",$url)) {	
		die("&nbsp;");
	}
	//check_gpc();
	 	
	$template_products_body="template/product_list.tpl";
	$template_contactList="template/contact_list.tpl";
	$template_memContact="template/memContact.tpl";
	$template_guestContact="template/guestContact.tpl";
	
	$base_url 	= 'index.php';
	
	$url= substr($url,strpos($url,".php"),strlen($url));
	$url=explode("/",$url);

	 $mode=$url[2] ; 

	 switch($mode){ 
		  case "delete":
			 deleteContact();
			 break;
		  case "send":
			  sendContact();
			  break;
		  case "gsend":
			  SendGuestContact();
			  break;
		  default:
			  viewcontact(); 
			  break; 
	 } 

function viewcontact(){
	global $template, $db, $template_products_body,$base_url,$site_base_href,$mode; 
		
	$type=isset($_POST["type"]) ? htmlspecialchars($_POST["type"]) : ''; 
	
	
	if($type=="addToCard" || $mode=="addToCard"){
		AddToBasket();
		return;
	}
	$ses_login=$_SESSION['ses_login'];
	if($ses_login["s_user"]){
		memContact();
		return;
	}else{
		guestContact();
		return;
	}	 
}
//============================================================================================================
function AddToBasket(){
	global $db,$template_contactList,$site_base_href,$base_url,$template,$ids ;
	$chkcontactonly	= isset($_POST["oldurl"]) ? htmlspecialchars($_POST["oldurl"]) : '';	

	addCart();
	 
	$ses_pro=$_SESSION["ses_pro"];
	$ses_pros=$_SESSION["ses_pros"];
	
	if(count($ses_pro)<1 && count($ses_pros)<1 ){
	  	redirect($site_base_href."/Inquiry.php?ids=".$ids."&lang=us");
	}
	redirect($site_base_href.'/index.php?ids='.$ids.'&lang=us&/contact/addToCard');
	return;
	/*{
	$sql='select s.memID,s.proID,s.ProductName,p.CompanyName,p.countryID from vb_products s inner join vb_member m on m.memID=s.memID inner join vb_memberprofile p on m.memID=p.memID
	where s.proID=-1';
	if(is_array($ses_pros)){
	
		while (list($id,) = each($ses_pros)){
			$id	= intval($id);
			if ($id){
				$sql.=" or s.proID=".$id;
			}
		}
		 
 
	if(!$result=$db->sql_query($sql)){
	  message_die("Couldn't run the sql query!!!","",__LINE__,__FILE__,$sql);
	}	
	$cust_count=$db->sql_numrows($result);
	$cust_data=$db->sql_fetchrowset($result);	
	$db->sql_freeresult($result);	
	
	for($i=0;$i<$cust_count;$i++){
			$template->assign_block_vars("ContactList", array(							
				'subject' =>$cust_data[$i]["ProductName"],
				'url' => $site_base_href.'/supplier.php?supplier='.$cust_data[$i]["memID"].'&act=vleads&mode=leadsdetail&lid='.$cust_data[$i]["lID"],
				'company'	=>$cust_data[$i]["CompanyName"],
				'country' =>getContry($cust_data[$i]["countryID"]),				
				'CHECKBOX'	 => '<input type="Checkbox" name="proid['.$cust_data[$i]["lID"].']" value="'. $cust_data[$i]["proID"] .'" checked />'
			));
		}
	}
	$template->assign_vars(array(	
		'S_ACTION' =>$site_base_href.'/index.php?/contacts',
		'c_delete' =>$site_base_href.'/index.php?/contacts/delete'
	));
	
	$template->set_filenames(array(
		'body' =>$template_contactList
		));
	$template->pparse('body');		
	}*/
}
function guestContact(){
	global $template,$template_guestContact,$base_url,$site_base_href,$ids ;
	$chkcontactonly	= isset($_POST["chkSes"]) ? intval($_POST["chkSes"]) : 0;	
	getContry();	
	addCart();		//Chuyen id sp vao session ses_pros1(Contact) or ses_pros(AddTocard)

	$i=rand(4,5);
	for($j=0;$j<$i;$j++){
	 	$id_random .= chr(rand(65,90));
	}
	if($chkcontactonly){
		$ses_pro=$_SESSION["ses_pro1"];
		$ses_pros=$_SESSION["ses_pros1"];
	}else{
		$ses_pro=$_SESSION["ses_pro"];
		$ses_pros=$_SESSION["ses_pros"];
	}
	if(is_array($ses_pro)){	
	 	$ses = count($ses_pro);
	}
	if(is_array($ses_pros)){	
	 	$ses += count($ses_pros);
	}
	if($ses>1){
		$comp='<a href="javascript:void(0)" onClick="displayDiv()">'.$ses.' Company </a>';
	}else{
		$comp=getCompany();
	}
	
	$template->assign_vars(array(
		'company' =>$comp,
		'ranima' => base64_encode(base64_encode($id_random)),
		'hdranima' =>$id_random,
		'con_company' =>getCompany(),
		'getImage' =>$site_base_href.'/',
		'chkSes'=>	$chkcontactonly,
		's_action'	=>$site_base_href."/".$base_url."?/contact/gsend/"		
	));
	
	$template->set_filenames(array(
		'body' =>$template_guestContact
	));
	$template->pparse('body');		
	 
}
function memContact(){
	global $template,$template_memContact,$site_base_href,$base_url;
	
	addCart();
	$chkcontactonly	= isset($_POST["chkSes"]) ? intval($_POST["chkSes"]) : 0;	
	if($chkcontactonly){
		$ses_pro=$_SESSION["ses_pro1"];
		$ses_pros=$_SESSION["ses_pros1"];
	}else{
		$ses_pro=$_SESSION["ses_pro"];
		$ses_pros=$_SESSION["ses_pros"];
	}
	if(is_array($ses_pro)){	
	 	$ses=count($ses_pro);
	}
	if(is_array($ses_pros)){	
	 	$ses+=count($ses_pros);
	}
	
	if($ses>1){
		$comp='<a href="javascript:void(0)" onClick="displayDiv()">'.$ses.' Company </a>';
	}else{
		$comp=getCompany();
	}
	$template->assign_vars(array(
		'company' =>$comp,
		'con_company' =>getCompany(),
		'chkSes'=>	$chkcontactonly,
		's_action'	=>$site_base_href."/".$base_url."?/contact/send/"		
	));
	
	$template->set_filenames(array(
		'body' =>$template_memContact
		));
	$template->pparse('body');		
	unset($ses_pros);

}
function getCompany(){
	global $db;
	
	$chkcontactonly	= isset($_POST["chkSes"]) ? intval($_POST["chkSes"]) : 0;	
	if($chkcontactonly){		
		$ses_pros=$_SESSION["ses_pros1"];
	}else{
		$ses_pros=$_SESSION["ses_pros"];
	}
	
	$comName='';

	$sql='select  m.CompanyName,p.proID from vb_memberprofile m inner join vb_products p on m.memID=p.memID where p.proID=-1';
		while (list(, $id) = each($ses_pros)){
			$id	= intval($id);
			if ($id){
				$sql.=" or p.proID=".$id;
			}
		}
		 
 
	if(!$result=$db->sql_query($sql)){
	  message_die("Couldn't run the sql query!!!","",__LINE__,__FILE__,$sql);
	}
	$comp=array();
	$cust_count=$db->sql_numrows($result);
	$cust_data=$db->sql_fetchrowset($result);	
	$db->sql_freeresult($result);	
	for($i=0;$i<$cust_count;$i++){
		$comp[$cust_data[$i]["proID"]]=$cust_data[$i]["CompanyName"];
	} 
	$cust_data=array();
	 if($chkcontactonly){		
		$ses_pros=$_SESSION["ses_pros1"];
	}else{		
		$ses_pros=$_SESSION["ses_pros"];
	}
		while (list(, $id) = each($ses_pros)){
			$id	= intval($id);			
			 
			if ($id){
				$comName.=$comp[$id]."<br>";
				
			}
	}
	if($chkcontactonly){
		$ses_pro=$_SESSION["ses_pro1"];		 
	}else{
		$ses_pro=$_SESSION["ses_pro"];
		 
	}
	if($ses_pro){
	$sql='select  m.CompanyName,p.proID from vb_memberprofile m inner join vb_products p on m.memID=p.memID where p.proID=-1';
		while (list(, $id) = each($ses_pro)){
			$id	= intval($id);
			if ($id){
				$sql.=" or p.proID=".$id;
			}
		}
 	 
	if(!$result=$db->sql_query($sql)){
	  message_die("Couldn't run the sql query!!!","",__LINE__,__FILE__,$sql);
	}
	$comp=array();
	$cust_count=$db->sql_numrows($result);
	$cust_data=$db->sql_fetchrowset($result);	
	$db->sql_freeresult($result);	
	for($i=0;$i<$cust_count;$i++){
		$comp[$cust_data[$i]["proID"]]=$cust_data[$i]["CompanyName"];
	}
	$cust_data=array();
	 	if($chkcontactonly){
		$ses_pro=$_SESSION["ses_pro1"];		 
	}else{
		$ses_pro=$_SESSION["ses_pro"];
		 
	}
		while (list(,$id) = each($ses_pro)){
			$id	= intval($id);			 
			if ($id){
				$comName.=$comp[$id]."<br>";
			}
	 }
	}	 
	
	return $comName;

}
function addCart(){	
	/* Chuyen cac id sp vao session	neu Contact chuyen vao bien ses_pros1 neu AddToCard chuyen vao bien ses_pros*/
	$proid	= isset($_POST["proid"]) ? $_POST["proid"] : '';
	$chkcontactonly	= isset($_POST["chkSes"]) ? intval($_POST["chkSes"]) : 0;	
	
	if ( !empty($proid) ){
		//Chuyen cac id san pham thanh mang
		if ( !is_array($proid) ){
			$pro_id[0]	= $proid;
		}else{
			$pro_id	= $proid;
		}
		unset($proid);
		
		if(!$HTTP_SESSION_VARS["ses_pros"]){
			session_register("ses_pros");		
		}	
		
		$ses_pros=$chkcontactonly ? '' :  $_SESSION["ses_pros"];
		
		if(count($ses_pro)>38){
			return;
		}
		while (list($id, $value) = each($pro_id)){
			$id	= intval($id);
			if ($id){
				$ses_pros[$id]=$value;					 
			}
		}	
		if($chkcontactonly){
	 		$_SESSION['ses_pros1']=$ses_pros;	
		}else{
			$_SESSION['ses_pros']=$ses_pros;	
		}
	}
	return;
}
function deleteContact(){	
	 global $site_base_href;
	$proid	= isset($_POST["proid"]) ? $_POST["proid"] : '';
	
	$ses_pros=$_SESSION["ses_pros"];		
	if ( !empty($proid) ){
		if ( !is_array($proid) ){
			$pro_id[0]	= $proid;
		}
		else{
			$pro_id	= $proid;
		}
		unset($proid);	
		
		while (list($id, $value) = each($pro_id)){
			$id	= intval($id);
			if ($value){
				unset($ses_pros[$id]);			
				
			}
		}		
		$_SESSION['ses_pros']=$ses_pros;		
	}
	
	redirect($site_base_href."/index.php?/contact/addToCard");
}
?>